package com.itheima.prize.api.action;

import com.itheima.prize.api.DTO.CardUserDTO;
import com.itheima.prize.api.convert.CardUserConverter;
import com.itheima.prize.commons.BO.CardUserBO;
import com.itheima.prize.commons.constant.AuthMessageConstants;
import com.itheima.prize.commons.constant.SystemConstants;
import com.itheima.prize.commons.db.entity.CardUser;
import com.itheima.prize.commons.db.service.CardUserService;
import com.itheima.prize.commons.db.service.GameLoadService;
import com.itheima.prize.commons.db.service.ViewCardUserHitService;
import com.itheima.prize.commons.utils.ApiResult;
import com.itheima.prize.commons.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/api/user")
@Api(tags = {"用户模块"})
public class UserController {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ViewCardUserHitService hitService;
    @Autowired
    private GameLoadService loadService;

    @Resource
    private CardUserService userService;


    @GetMapping("/info")
    @ApiOperation(value = "用户信息")
    public ApiResult info(HttpServletRequest request) {
        CardUser cardUser = (CardUser) request.getSession().getAttribute("user");
        CardUserBO cardUserBO = userService.getCardUserInfo(cardUser);
        CardUserDTO cardUserDTO = CardUserConverter.INSTANCE.convertCardUserDtoToBO(cardUserBO);
        return ApiResult.success(SystemConstants.OPERATION_SUCCESS, cardUserDTO);
    }

    @GetMapping("/hit/{gameId}/{curPage}/{limit}")
    @ApiOperation(value = "我的奖品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gameId", value = "活动id（-1=全部）", dataType = "int", example = "1", required = true),
            @ApiImplicitParam(name = "curPage", value = "第几页", defaultValue = "1", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "limit", value = "每页条数", defaultValue = "10", dataType = "int", example = "3")
    })
    public ApiResult hit(@PathVariable int gameId, @PathVariable int curPage, @PathVariable int limit, HttpServletRequest request) {
        CardUser user = (CardUser) request.getSession().getAttribute("user");
        // 根据用户信息查询我的奖品
        return hitService.getPrizeByUserInfo(user, gameId, curPage, limit);
    }


}